package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.common.Contants;
import com.qeeyou.apps.android.craftsman.view.PayMethodButton;

public class PayActivity extends Activity {

    private ImageView btnBack;
    private TextView txtvPayNo;
    private TextView txtvPayMoney;
    private PayMethodButton btnWeiXin;
    private PayMethodButton btnAliPay;
    private Button btnConfirm;
    private Button btnCancel;
    private String mPriceStr;
    private String mPayMethod;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);

        if(getIntent().getStringExtra(Contants.Extra.EXTRA_INSTALL_PRICE) != null) {
            mPriceStr = getIntent().getStringExtra(Contants.Extra.EXTRA_INSTALL_PRICE);
        }

        setView();
        setEvent();

    }

    private void setView() {
        btnBack = (ImageView) this.findViewById(R.id.btnBack4Pay);
        txtvPayNo = (TextView) this.findViewById(R.id.txtvPayNo4Pay);
        txtvPayMoney = (TextView) this.findViewById(R.id.txtvPayMoney4Pay);
        btnWeiXin = (PayMethodButton) this.findViewById(R.id.btnWeiXin4Pay);
        btnAliPay = (PayMethodButton)this.findViewById(R.id.btnAlipay4Pay);
        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4Pay);
        btnCancel = (Button) this.findViewById(R.id.btnCancel4Pay);
    }

    private void setEvent() {

        if(mPriceStr != null) {
            txtvPayMoney.setText(mPriceStr + "元");
        } else {
            txtvPayMoney.setText("20400元");
        }
        mPayMethod = null;

        btnBack.setOnClickListener(onBackClickListener);

        btnWeiXin.setOnClickListener(onMethodClickListener);
        btnAliPay.setOnClickListener(onMethodClickListener);

        btnConfirm.setOnClickListener(onConfirmClickListener);
        btnCancel.setOnClickListener(onCancelClickListener);
    }

    //返回按钮
    private View.OnClickListener onBackClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PayActivity.this.finish();
        }
    };

    private View.OnClickListener onMethodClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(view == btnWeiXin) {
                btnWeiXin.setSelected(true);
                btnAliPay.setSelected(false);
                mPayMethod = Contants.PayMethod.WEIXIN;
            } else {
                btnWeiXin.setSelected(false);
                btnAliPay.setSelected(true);
                mPayMethod = Contants.PayMethod.ALIPAY;
            }
        }
    };

    //确认支付
    private View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //TODO -- 确认支付
            if(mPayMethod == null) {
                Toast.makeText(PayActivity.this, "请选择支付方式", Toast.LENGTH_SHORT).show();
                return;
            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(PayActivity.this, "支付成功!", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(PayActivity.this, PublishSuccessActivity.class);
                    PayActivity.this.startActivity(intent);
                    PayActivity.this.finish();
                }
            }, 800);

        }
    };

    //取消
    private View.OnClickListener onCancelClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    PayActivity.this.finish();
                }
            }, 800);
        }
    };
}
